package com.jay.tools.base;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.jay.tools.R;
import com.jay.tools.utils.ToastUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * author: tanjianpei
 * date: 2018/4/17 017 9:09
 * <p>
 * description:
 */
public abstract class BaseDialogFragment extends DialogFragment {

    protected String TAG = this.getClass().getSimpleName();

    private Unbinder mBinder;

    protected Activity mActivity;

    protected boolean mCanceledOnTouchOutside = false;
    protected boolean mShowBottom = false;

    protected OnDestroyListener mListener;

    public void setOnDestroyListener(OnDestroyListener listener) {
        mListener = listener;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mActivity = null;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        initPresenter();
        Dialog dialog = new Dialog(getActivity(), R.style.CustomDatePickerDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // must be called before set content

        if (getLayout() instanceof Integer) {
            dialog.setContentView((Integer) getLayout());
        } else {
            dialog.setContentView((View) getLayout());
        }

        Window window = dialog.getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        if (mShowBottom) {
            WindowManager.LayoutParams wlp = window.getAttributes();
            // 设置宽度为屏宽、靠近屏幕底部。
            wlp.gravity = Gravity.BOTTOM;
            wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
            window.setAttributes(wlp);
        }

        mBinder = ButterKnife.bind(this, dialog);

        initDatas(savedInstanceState);

        dialog.setCanceledOnTouchOutside(mCanceledOnTouchOutside);

        initEvents(dialog);

        return dialog;
    }

    protected void initPresenter() {

    }

    protected abstract Object getLayout();

    protected abstract void initDatas(Bundle savedInstanceState);

    protected void initEvents(Dialog dialog) {
        // 所有的事件监听
    }

    public void showDialog(FragmentManager manager) {
        manager.beginTransaction()
                .add(this, this.getClass().getSimpleName())
                .commitAllowingStateLoss();
    }

    protected void toast(Object msg) {
        if (mActivity == null) {
            return;
        }
        if (msg instanceof Integer) {
            ToastUtil.toast(mActivity, (Integer) msg);
        } else {
            ToastUtil.toast(mActivity, msg.toString());
        }
    }

    protected void toastLong(Object msg) {
        if (mActivity == null) {
            return;
        }
        if (msg instanceof Integer) {
            ToastUtil.toastLong(mActivity, (Integer) msg);
        } else {
            ToastUtil.toastLong(mActivity, msg.toString());
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mBinder.unbind();
        if (null != mListener) {
            mListener.onDestroy(null, false);
        }
    }

    public interface OnDestroyListener {
        void onDestroy(Object object, boolean isChange);
    }

}
